<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

App::import('Vendor', 'cart/jproduct');

class CartController extends AppController {

    var $layout = 'default';
    var $uses = array(
        'Product'
    );
    var $components = array(
        'Email'
    );
    var $scripts = array('jquery-1.7.1.js', 'slideshowNews.js', 'common.js');

    function order() {
        $this->set('title_for_layout', 'Cám ơn bạn đã đặt hàng - www.thoitrangred.com');
        if (!$this->cart) {
            $this->redirect(array('controller' => 'trangchu.html'));
            return;
        }

        $data = $this->params['form'];

        $smtpOptions = array(
            'port' => Configure::read('smtpport'),
            'timeout' => Configure::read('smtptimeout'),
            'host' => Configure::read('smtphost'),
            'username' => Configure::read('smtpuser'),
            'password' => Configure::read('smtppass')
        );

        $this->Email->smtpOptions = $smtpOptions;
        $this->Email->delivery = Configure::read('mailmethod');
        $this->Email->to = $data['txtEmail'];
        $this->Email->from = Configure::read('email_admin');
        $this->Email->subject = "Thông tin đặt hàng của bạn tại shop thời trang RED";
        $this->Email->template = 'buy_client';
        $this->Email->sendAs = 'html';
        $this->set('cart', $this->cart);
        $this->Email->send();

        //send to admin

        $this->Email->to = Configure::read('email_admin');
        $this->Email->from = Configure::read('email_admin');
        $this->Email->subject = "Thông tin đặt hàng mới";
        $this->Email->template = 'buy_admin';
        $this->Email->sendAs = 'html';
        $this->set('cart', $this->cart);
        $this->set('mail_content', $data['txtContent']);
        $this->set('address', $data['txtAddress']);
        $this->set('phone', $data['txtPhone']);
        $this->set('mail_fullname', $data['txtName']);
        $this->set('mail_email', $data['txtEmail']);
        $this->Email->send();

    }

    function buy() {
        $this->set('title_for_layout', 'Điền thông tin đặt hàng - www.thoitrangred.com');
        if (!$this->cart) {
            $this->redirect(array('controller' => 'trangchu.html'));
            return;
        }
        /*


         */
    }

    function delete() {
        $data = $this->params['form'];
        $this->layout = null;
        $this->cart->deleteItem($data['product_cart_id']);
        $this->cart->storeCart($this->Cookie);
        $this->redirect(array('controller' => 'cart', 'action' => 'index'));
    }

    function update() {
        $data = $this->params['form'];
        $this->layout = null;
        foreach ($data['quantity'] AS $key => $value) {
            $this->cart->updateItem($key, array('quantity' => intval($value)));
            $this->cart->storeCart($this->Cookie);
        }
        $this->redirect(array('controller' => 'cart', 'action' => 'index'));
    }

    function add() {
        $data = $this->params['form'];
        $this->layout = null;

        $product = new jproduct();
        $array = array(
            'product_name' => $data['product_name'],
            'product_id' => $data['product_id'],
            'product_cart_id' => $data['product_cart_id'],
            'price' => $data['price'],
            'image' => $data['thumbnail'],
            'quantity' => $data['quantity'],
            'url' => $data['url']
        );

        $product->bind($array);

        $this->cart->addItem($product);
        $this->cart->storeCart($this->Cookie);
        $this->redirect(array('controller' => 'cart', 'action' => 'index'));
    }

    function index() {
        $this->set('title_for_layout', 'Gio hang - www.thoitrangred.com');
        $this->set('cart', $this->cart);
    }

}

?>
